$(function () {

    var parentId = 0;
    getSubjectList(parentId);

    $('body').on('click','#filter-subject-parent li',function () {//点击专业筛选科目
        $(this).addClass('active').siblings().removeClass('active');
        parentId = $("#filter-subject-parent").find('li.active').data('id');
        getSubjectList(parentId);
    }).on('click','.col-auto',function () {//点击科目，查询课程列表
        $(this).addClass('active').siblings().removeClass('active');
        var childId = $(this).data('id');
        var subjectIds = parentId + '';
        if(typeof childId != 'undefined'){
            subjectIds += "," + childId;
        }
        window.location.href = baseloaction + '/mobile/course/courseList?subjectIds='+ subjectIds;
    });

    //获取父级ID查询专业科目
    function getSubjectList(parentId) {
        var params = {"parentId":parentId};
        params = JSON.stringify(params);
        $.ajax({
            url: baseloaction + '/mobile/subject/ajax/findLayeredSubjectList',
            data: params,
            type: 'post',
            dataType: 'json',
            contentType: 'application/json;charset=utf-8',
            success: function (result) {
                if(result.success){
                    var list = result.entity==null? null : result.entity[parentId];
                    if(parentId == 0){
                        setParentSubject(list);
                    }else {
                        setChildSubject(list);
                    }
                }
            },
            error: function (error) {
                $.toast('系统繁忙，请稍后重试');
            }
        })
    }

    //设置左侧专业
    function setParentSubject(list) {
        if(list !=null && list.length >0){
            var _html = "<li data-id='' class='active'>全部</li>";
            $.each(list, function (index, ele) {
                    _html += "<li data-id='"+ ele.id +"'>"+ ele.subjectName +"</li>"
            });
            $("#filter-subject-parent").html(_html);

            parentId = $("#filter-subject-parent").find('li.active').data('id');
            if(parentId==null|| parentId==""){
                var _html = "<li class='col-auto active'><a href='javascript:void(0)' class='button button-round'>全部</a></li>";
                $("#filter-subject-son").html(_html);
                return
            }
            getSubjectList(parentId);
        }
    }

    //设置右侧科目
    function setChildSubject(list) {
        var _html = "<li class='col-auto active'><a href='javascript:void(0)' class='button button-round'>全部</a></li>";
        if(list != null && list.length >0){
            $.each(list, function (index, ele) {
                _html += "<li class='col-auto' data-id='"+ ele.id +"'><a href='javascript:void(0)' class='button button-round'>"+ ele.subjectName +"</a></li>";
            })
        }
        $("#filter-subject-son").html(_html);
    }
})